package com.wx.mall.order.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.common.utils.DataResult;
import com.wx.mall.common.vo.order.OrderReCauseVo;
import com.wx.mall.order.entity.ReturnCause;
import com.wx.mall.order.service.impl.ReturnCauseServiceImpl;
import com.wx.mall.order.vo.ReturnCauseVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 退款原因 前端控制器
 * </p>
 *
 * @author j
 * @since 2020-04-13
 */
@Slf4j
@RestController
@RequestMapping("/return/cause")
public class ReturnCauseController {

    @Autowired
    private ReturnCauseServiceImpl returnCauseService;

    @ApiOperation(value = "分页查询，根据id查询")
    @GetMapping("/page")
    public CommonResult<CommonPage<ReturnCause>> getPages(Integer pageNum, Integer pageSize,int seq){
        if (pageNum == null || pageSize==null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (pageNum == 0){
            ApiAssert.fail("无效页码");
        }
        List<ReturnCause> returnCauseList=returnCauseService.find(pageNum,pageSize,seq);
        return CommonResult.success(CommonPage.restPage(returnCauseList));
    }

    @ApiOperation(value = "新增")
    @PostMapping("/addCause")
    public CommonResult<Integer> adds(ReturnCauseVo vo){
        int save1=returnCauseService.addSaveList(vo);
        return save1==1?CommonResult.success():CommonResult.failed();
    }

    @ApiOperation(value = "根据id编辑修改")
    @PutMapping("/updates")
    public CommonResult<Integer> updateEdit(ReturnCauseVo vo){
        int upd=returnCauseService.updateList(vo);
        return upd==1?CommonResult.success():CommonResult.failed();
    }

    @ApiOperation(value = "根据id单条删除")
    @GetMapping("/deletes")
    public CommonResult<Integer> deleteCause(Integer id){
        int dels=returnCauseService.deleteList(id);
        return dels==1?CommonResult.success():CommonResult.failed();
    }

    @ApiOperation(value ="根据id批量删除")
    @DeleteMapping("/deletesAll")
    public CommonResult<Integer> deleteAllList(OrderReCauseVo vo){
        log.info(vo.toString());
        int success=returnCauseService.deleteALLList(vo);
        return success==1?CommonResult.success():CommonResult.failed();
    }

}

